package com.linglan.modules.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linglan.common.api.Condition;
import com.linglan.common.api.Page;
import com.linglan.common.api.Query;
import com.linglan.common.utils.PageUtil;
import com.linglan.modules.feedback.dao.BizFeedbackDao;
import com.linglan.modules.feedback.dto.result.FeedbackResult;
import com.linglan.modules.feedback.entity.BizFeedback;
import com.linglan.modules.feedback.service.BizFeedbackService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务-反馈评价表(BizFeedback)表服务实现类
 *
 * @author makejava
 * @since 2025-05-05 20:43:39
 */
@Service("bizFeedbackService")
public class BizFeedbackServiceImpl extends ServiceImpl<BizFeedbackDao, BizFeedback> implements BizFeedbackService {

    /**
     * 分页查询
     *
     * @param query
     * @param departmentId
     * @return
     */
    @Override
    public Page<FeedbackResult> findByPage(Query query, Long departmentId) {
        LambdaQueryWrapper<BizFeedback> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizFeedback::getIsDeleted, 0);
        if (departmentId != null) {
            queryWrapper.eq(BizFeedback::getDepartmentId, departmentId);
        }
        queryWrapper.orderByDesc(BizFeedback::getCreatedAt);
        IPage<BizFeedback> page = this.page(Condition.getPage(query), queryWrapper);
        IPage<FeedbackResult> feedbackResultIPage = PageUtil.copyPurePage(page, FeedbackResult.class);
        List<BizFeedback> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            List<FeedbackResult> resultList = new ArrayList<>();
            for (BizFeedback record : records) {
                FeedbackResult feedbackResult = getFeedbackResult(record);
                resultList.add(feedbackResult);
            }
            feedbackResultIPage.setRecords(resultList);
        }
        return Page.of(feedbackResultIPage);
    }

    private static FeedbackResult getFeedbackResult(BizFeedback record) {
        FeedbackResult feedbackResult = new FeedbackResult();
        feedbackResult.setId(record.getId());
        feedbackResult.setUserId(record.getUserId());
        feedbackResult.setDoctorId(record.getDoctorId());
        feedbackResult.setDepartmentId(record.getDepartmentId());
        feedbackResult.setScore(record.getScore());
        feedbackResult.setContent(record.getContent());
        feedbackResult.setCreatedAt(record.getCreatedAt());
        return feedbackResult;
    }
}

